<script setup>
import { ref, onMounted } from 'vue'
import { useWardrobesStore } from '../stores/wardrobes'
import { ElMessage } from 'element-plus'
import { Picture, VideoPlay, Edit, Check } from '@element-plus/icons-vue'

const store = useWardrobesStore()
const submitting = ref(false)
const marking = ref(false)
const isDesktop = ref(false)

const form = ref({
  id: '',
  sale_price: 0,
  description: '',
  images: [],
  videos: [],
})

function add(list, v) { 
  if (v && v.trim()) {
    list.value.push(v.trim())
    ElMessage.success('媒体URL已添加')
  }
}

async function submit() {
  if (!form.value.id || !form.value.sale_price) {
    ElMessage.error('请填写衣柜ID和售价')
    return
  }
  
  submitting.value = true
  try {
    await store.updateListing(Number(form.value.id), { 
      sale_price: Number(form.value.sale_price), 
      description: form.value.description, 
      images: form.value.images, 
      videos: form.value.videos 
    })
    
    ElMessage.success('上架信息更新成功！')
    form.value.sale_price = 0
    form.value.description = ''
    form.value.images = []
    form.value.videos = []
  } catch (error) {
    ElMessage.error('更新失败：' + error.message)
  } finally {
    submitting.value = false
  }
}

async function markListed() {
  if (!form.value.id) {
    ElMessage.error('请输入衣柜ID')
    return
  }
  
  marking.value = true
  try {
    await store.markListed(Number(form.value.id))
    ElMessage.success('衣柜已标记为已上架！')
    form.value.id = ''
  } catch (error) {
    ElMessage.error('标记失败：' + error.message)
  } finally {
    marking.value = false
  }
}

// 检测设备类型
function detectDevice() {
  const userAgent = navigator.userAgent.toLowerCase()
  const isMobile = /mobile|android|iphone|ipad|phone/i.test(userAgent)
  const isTablet = /tablet|ipad/i.test(userAgent)
  const screenWidth = window.innerWidth
  
  isDesktop.value = !isMobile && !isTablet && screenWidth > 1024
}

onMounted(() => {
  detectDevice()
  window.addEventListener('resize', detectDevice)
})
</script>

<template>
  <div class="listing-container" :class="{ 'desktop': isDesktop }">
    <div class="page-header">
      <h1>✨ 待上架 - 精修与定价</h1>
      <p>为待上架衣柜添加精修媒体、设置售价和描述信息</p>
    </div>

    <el-card class="form-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <span class="card-title">上架信息设置</span>
          <el-tag type="success" size="small">待上架状态</el-tag>
        </div>
      </template>

      <el-form :model="form" label-width="120px" class="listing-form">
        <el-form-item label="衣柜ID" required>
          <el-input 
            v-model="form.id" 
            placeholder="请输入衣柜ID"
            class="form-input"
          />
        </el-form-item>

        <el-form-item label="售卖价格" required>
          <el-input-number 
            v-model="form.sale_price" 
            :min="0" 
            :precision="2"
            placeholder="0.00"
            class="form-input"
          />
          <span class="unit">元</span>
        </el-form-item>

        <el-form-item label="描述信息">
          <el-input 
            type="textarea" 
            v-model="form.description" 
            placeholder="请输入衣柜的详细描述信息"
            :rows="4"
            class="form-input"
          />
        </el-form-item>

        <el-divider content-position="left">
          <span class="divider-title">📸 精修媒体</span>
        </el-divider>

        <div class="media-section">
          <el-form-item label="精修图片">
            <div class="url-input-group">
              <el-input 
                @keyup.enter="addImage($event.target.value); $event.target.value=''" 
                placeholder="输入精修图片URL后按回车添加"
                class="url-input"
              />
              <el-button type="primary" @click="addImage($event.target.previousElementSibling.value); $event.target.previousElementSibling.value=''">
                添加
              </el-button>
            </div>
            <div class="media-tags">
              <el-tag 
                v-for="(url, i) in form.images" 
                :key="i" 
                closable 
                @close="form.images.splice(i,1)"
                type="success"
                class="media-tag"
              >
                <el-icon><Picture /></el-icon>
                {{ url.length > 30 ? url.substring(0, 30) + '...' : url }}
              </el-tag>
            </div>
          </el-form-item>

          <el-form-item label="精修视频">
            <div class="url-input-group">
              <el-input 
                @keyup.enter="addVideo($event.target.value); $event.target.value=''" 
                placeholder="输入精修视频URL后按回车添加"
                class="url-input"
              />
              <el-button type="primary" @click="addVideo($event.target.previousElementSibling.value); $event.target.previousSibling.value=''">
                添加
              </el-button>
            </div>
            <div class="media-tags">
              <el-tag 
                v-for="(url, i) in form.images" 
                :key="i" 
                closable 
                @close="form.videos.splice(i,1)"
                type="info"
                class="media-tag"
              >
                <el-icon><VideoPlay /></el-icon>
                {{ url.length > 30 ? url.substring(0, 30) + '...' : url }}
              </el-tag>
            </div>
          </el-form-item>
        </div>

        <el-form-item class="submit-section">
          <el-button 
            type="primary" 
            size="large" 
            @click="submit"
            :loading="submitting"
            class="submit-btn"
          >
            <el-icon><Edit /></el-icon>
            更新上架信息
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="form-card" shadow="hover" style="margin-top: 20px;">
      <template #header>
        <div class="card-header">
          <span class="card-title">标记为已上架</span>
          <el-tag type="warning" size="small">状态变更</el-tag>
        </div>
      </template>

      <div class="mark-listed-section">
        <p class="section-desc">将衣柜状态从"待上架"变更为"已上架"，开始销售</p>
        <div class="action-group">
          <el-input 
            v-model="form.id" 
            placeholder="请输入衣柜ID" 
            class="action-input"
          />
          <el-button 
            type="success" 
            size="large"
            @click="markListed"
            :loading="marking"
            class="action-btn"
          >
            <el-icon><Check /></el-icon>
            标记已上架
          </el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.listing-container {
  max-width: 1000px;
  margin: 0 auto;
}

.listing-container.desktop {
  max-width: none;
  margin: 0;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
  color: white;
}

.page-header h1 {
  margin: 0 0 10px 0;
  font-size: 2.5rem;
  font-weight: 700;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.page-header p {
  margin: 0;
  font-size: 1.1rem;
  opacity: 0.9;
}

.form-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: #303133;
}

.listing-form {
  padding: 20px 0;
}

.form-input {
  width: 100%;
}

.unit {
  margin-left: 8px;
  color: #909399;
  font-size: 14px;
}

.divider-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: #606266;
}

.media-section {
  margin: 20px 0;
}

.url-input-group {
  display: flex;
  gap: 10px;
  margin-bottom: 15px;
}

.url-input {
  flex: 1;
}

.media-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.media-tag {
  max-width: 300px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.submit-section {
  text-align: center;
  margin-top: 40px;
}

.submit-btn {
  padding: 15px 40px;
  font-size: 1.1rem;
  border-radius: 25px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.mark-listed-section {
  padding: 20px 0;
}

.section-desc {
  color: #606266;
  margin-bottom: 20px;
  text-align: center;
}

.action-group {
  display: flex;
  gap: 15px;
  align-items: center;
  justify-content: center;
}

.action-input {
  width: 300px;
}

.action-btn {
  padding: 12px 30px;
  border-radius: 20px;
  box-shadow: 0 3px 10px rgba(103, 194, 58, 0.3);
  transition: all 0.3s ease;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(103, 194, 58, 0.4);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header h1 {
    font-size: 2rem;
  }
  
  .action-group {
    flex-direction: column;
    gap: 15px;
  }
  
  .action-input {
    width: 100%;
  }
  
  .submit-btn {
    width: 100%;
    padding: 15px 20px;
  }
}

@media (max-width: 480px) {
  .page-header h1 {
    font-size: 1.8rem;
  }
  
  .page-header p {
    font-size: 1rem;
  }
  
  .form-card {
    margin: 0 10px;
  }
  
  .url-input-group {
    flex-direction: column;
  }
}
</style>


